package gov.pbc.nn.kjgl.archive.dao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;

import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.kjgl.entity.Archives;
import gov.pbc.nn.kjgl.entity.ArchivesMember;

@Repository
public class ArchivesTwoDao extends AbstractHibernateDao {

	/**
	 * 获取一级档案
	 * 
	 * @return
	 */
	public List<Archives> getArchives() {
		List<Archives> resutls = new ArrayList<Archives>();
		String hql = "from Archives a where a.invalid=false and a.id in (select archivesId from ArchivesMember where type=1 and memberId=:username and invalid=false)";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", ContextHolder.getLoginUserName());
		List<String> isExits = new ArrayList<String>();
		List<Archives> ls = this.query(hql, map);
		for (Archives archive : ls) {
			if (StringUtils.isNotEmpty(archive.getParentId())) {
				if (!isExits.contains(archive.getParentId())) {
					resutls.add(getArchivesById(archive.getParentId()));
					isExits.add(archive.getParentId());
				}
			} else {
				resutls.add(archive);
				isExits.add(archive.getId());
			}
		}
		return resutls;
	}

	public Archives getArchivesById(String id) {
		String hql = "from Archives where invalid=false and id=:id";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		return (Archives) this.query(hql, map).get(0);

	}

//	/**
//	 * 获取二级档案
//	 * 
//	 * @param parentId
//	 * @return
//	 */
//	public List<Archives> getArchivesByParentId(String parentId) {
//		String hql = "from Archives where parentId=:parentId and invalid=false and id in (select archivesId from ArchivesMember where invalid=false and type=1 and memberId=:username) ";
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("parentId", parentId);
//		map.put("username", ContextHolder.getLoginUserName());
//		return this.query(hql, map);
//	}
	/**
	 * 获取二级档案
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Archives> getArchivesByParentId(String parentId) {
		String hql = "from Archives where parentId=:parentId and invalid=false";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentId", parentId);
		List<Archives> ls = this.query(hql, map);
		if (ls != null) {
			return ls;
		}
		return null;
	}

	/**
	 * 保存，删除档案
	 * 
	 * @param archives
	 */
	public void saveArchives(Collection<Archives> archives) {
		for (Archives arc : archives) {
			EntityState state = EntityUtils.getState(arc);
			if (state.equals(EntityState.NEW)) {
				this.save(arc);
				ArchivesMember arcm = new ArchivesMember();
				arcm.setArchivesId(arc.getId());
				arcm.setMemberId(ContextHolder.getLoginUserName());
				arcm.setType(1);
				this.save(arcm);
			} else if (state.equals(EntityState.DELETED)) {
				this.delete(arc);
				if(getArcM(arc)!=null){
					this.delete(getArcM(arc));
				}
			}
			if (arc.getChildren() != null) {
				Archives[] arcs = new Archives[arc.getChildren().size()];
				saveArchives(Arrays.asList(arc.getChildren().toArray(arcs)));
			}
		}
	}

	/**
	 * 判断同一级节点下 有没有相同的名字
	 * @param id
	 * @return
	 */
	public boolean checkName(String parentId,String name){
		String hql="select count(*) from Archives where invalid=false and name=:name and parentId=:parentId";
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("name", name);
		map.put("parentId", parentId);
		int a=this.queryForInt(hql,map);
		if(a==0){
			return true;
		}
		return false;
	}
	
	/**
	 * 根据档案查档案关系表
	 * 
	 * @param arc
	 * @return
	 */
	public ArchivesMember getArcM(Archives arc) {
		String hql = "from ArchivesMember where invalid=false and type=1 and memberId=:username and archivesId=:archivesId";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", ContextHolder.getLoginUserName());
		map.put("archivesId", arc.getId());
		List<ArchivesMember> list =this.query(hql, map);
		if(list.size()!=0){
			return list.get(0);
		}else{
			return null;
		}
	}

}
